package com.zhuoyue.listener;

import com.zhuoyue.biz.core.api.domain.SetSolVoiceScene;
import com.zhuoyue.biz.core.api.domain.SetSolVoiceVirtualDevice;
import com.zhuoyue.biz.core.api.service.impl.SetSolVoiceSceneService;
import com.zhuoyue.biz.core.api.service.impl.SetSolVoiceVirtualDeviceService;
import net.sf.json.JSONObject;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author Administrator
 */
@Component
@RabbitListener(queues = "${topic.rabbit.sol.voice}")
public class SolVoiceListen {

    @Autowired
    private SetSolVoiceSceneService setSolVoiceSceneService;

    @Autowired
    private SetSolVoiceVirtualDeviceService setSolVoiceVirtualDeviceService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 数据下发操作
     *
     * @param frameAndChannel
     */
    @RabbitHandler
    public void dataDistribution(Map<String, Object> frameAndChannel) {
        try {
            //虚拟设备
            String odV = "1B5B";
            //场景
            String odScene = "1B59";
            String zigbeeMac = (String) frameAndChannel.get("deviceMac");
            String od = (String) frameAndChannel.get("od");
            //虚拟设备
            if (odV.equals(od)) {
                String addKey = zigbeeMac + "_" + od + "_add";
                String updateKey = zigbeeMac + "_" + od + "_update";
                Object add = redisTemplate.opsForValue().get(addKey);
                Object up = redisTemplate.opsForValue().get(updateKey);
                SetSolVoiceVirtualDevice setSolVoiceVirtualDevice = null;
                if (add != null) {
                    setSolVoiceVirtualDevice = (SetSolVoiceVirtualDevice) JSONObject.toBean(JSONObject.fromObject(add), SetSolVoiceVirtualDevice.class);
                    setSolVoiceVirtualDeviceService.addVirtualDevice(setSolVoiceVirtualDevice);
                    redisTemplate.delete(addKey);
                    return;
                } else if (up != null) {
                    setSolVoiceVirtualDevice = (SetSolVoiceVirtualDevice) JSONObject.toBean(JSONObject.fromObject(up), SetSolVoiceVirtualDevice.class);
                    setSolVoiceVirtualDeviceService.putVoiceVirtualDeviceRelation(setSolVoiceVirtualDevice);
                    redisTemplate.delete(updateKey);
                    return;
                } else {
                    return;
                }

            }
            //场景配置
            if (odScene.equals(od)) {
                String odCode = (int) frameAndChannel.get("odCode") + "";
                String addKey = zigbeeMac + "_" + odCode + "_add";
                String updateKey = zigbeeMac + "_" + odCode + "_update";
                Object add = redisTemplate.opsForValue().get(addKey);
                Object up = redisTemplate.opsForValue().get(updateKey);
                SetSolVoiceScene setSolVoiceScene = null;
                if (add != null) {
                    setSolVoiceScene = (SetSolVoiceScene) JSONObject.toBean(JSONObject.fromObject(add), SetSolVoiceScene.class);
                    setSolVoiceSceneService.saveMessage(setSolVoiceScene);
                    redisTemplate.delete(addKey);
                    return;
                } else if (up != null) {
                    setSolVoiceScene = (SetSolVoiceScene) JSONObject.toBean(JSONObject.fromObject(up), SetSolVoiceScene.class);
                    setSolVoiceSceneService.putSceneRelation(setSolVoiceScene);
                    redisTemplate.delete(updateKey);
                    return;
                }

            }
        } catch (Exception e) {

        }
    }
}
